#include "../src/biTree/biTree.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern func_BiTree fBiTree;

#define BiTNodeNUMMAX 16

int main(int argc, const char *argv[])
{
    int ret = 0;
    BiTNode *tree = NULL;

    unsigned char inOdBuf[BiTNodeNUMMAX] = {0}, postOdBuf[BiTNodeNUMMAX] = {0};

    printf("请输入中序遍历字符串：");
    fgets((char *)inOdBuf, sizeof(inOdBuf), stdin);
    printf("请输入后序遍历字符串：");
    fgets((char *)postOdBuf, sizeof(postOdBuf), stdin);

    if (strlen((char *)inOdBuf) != strlen((char *)postOdBuf) ||
        strlen((char *)inOdBuf) > BiTNodeNUMMAX ||
        strlen((char *)postOdBuf) > BiTNodeNUMMAX)
    {
        printf("输入字符串信息异常或者不匹配，请检查并重新运行！\n");
        ret = -1;
        goto ERROR_END;
    }

    // 创建一个二叉树
    tree = fBiTree.create(inOdBuf, postOdBuf, strlen((char *)inOdBuf));

    if (tree != NULL)
    {
        printf("二叉树创建成功！\n");
    }
    else
    {
        printf("二叉树创建出现异常！\n");
        ret = -1;
        goto ERROR_END;
    }

    printf("先序遍历输出：");
    fBiTree.prevOrder(tree);
    putchar(10);

    printf("中序遍历输出：");
    fBiTree.inOrder(tree);
    putchar(10);

    printf("后序遍历输出：");
    fBiTree.postOrder(tree);
    putchar(10);

    printf("层序遍历输出：");
    fBiTree.levelOrder(tree);
    putchar(10);

ERROR_END:
    // 释放二叉树
    fBiTree.release(tree);
    printf("system exit with return code %d\n", ret);

    return ret;
}
